package short_semester.questionnaire_planet.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import short_semester.questionnaire_planet.entity.User;
import short_semester.questionnaire_planet.request.fillRequest.ip.IPBody;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(indexes = {@Index(columnList = "uuid")})
@Data
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
@EntityListeners(AuditingEntityListener.class)
public class QuestionnaireRecord {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private Questionnaire questionnaire;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private User user;

    @Column
    private Boolean isSubmitted = false;
    
    @Column
    private String uuid;
    
    @CreatedDate    //创建日期
    @Column(updatable = false)
    @JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createdDate;

    @Column
    @JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date submitDate;
    
    @Column
    private Long consTime = 0L;
    private Integer grade = 0;
    
    @Column
    private String ip = "";
    
    @Column
    private String province = "";
    
    @Column
    private String city = "";
    
    public void setIPBody(IPBody ipBody){
        if(ipBody.getStatus()==0){
            ip = ipBody.getResult().getIp();
            province = ipBody.getResult().getAd_info().getProvince();
            city = ipBody.getResult().getAd_info().getCity();
        }
    }
}
